/*
* @(#)ListStaffHierarchyAction.java
*
* Copyright (c) 2003 DCIVision Ltd
* All rights reserved.
*
* This software is the confidential and proprietary information of DCIVision
* Ltd ("Confidential Information").  You shall not disclose such Confidential
* Information and shall use it only in accordance with the terms of the license
* agreement you entered into with DCIVision Ltd.
*/

package com.dcivision.staff.web;

import java.sql.Connection;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.dcivision.framework.ApplicationException;
import com.dcivision.framework.SessionContainer;
import com.dcivision.framework.SystemFunctionConstant;
import com.dcivision.framework.TextUtility;
import com.dcivision.framework.Utility;
import com.dcivision.framework.web.AbstractListAction;
import com.dcivision.framework.web.AbstractSearchForm;
import com.dcivision.staff.bean.StaffHierarchy;
import com.dcivision.staff.dao.StaffHierarchyDAObject;
import com.dcivision.staff.dao.StaffRecordDAObject;

/**
 * <p>Class Name:       ListStaffHierarchyAction.java    </p>
 * <p>Description:      The list action class for ListStaffHierarchy.jsp</p>
 *
 *    @author           Wong Yam Lee
 *    @company          DCIVision Limited
 *    @creation date    28/07/2003
 *    @version          $Revision: 1.8 $
 */

public class ListStaffHierarchyAction extends AbstractListAction {

  public static final String REVISION = "$Revision: 1.8 $";


  /**
   *  Constructor - Creates a new instance of ListStaffHierarchyAction and define the default listName.
   */
  public ListStaffHierarchyAction() {
    super();
    this.setListName("staffHierarchyList");
  }

  /**
   * getMajorDAOClassName
   *
   * @return  The class name of the major DAObject will be used in this action.
   */
  public String getMajorDAOClassName() {
    return("com.dcivision.staff.dao.StaffHierarchyDAObject");
  }

  /**
   * getFunctionCode
   *
   * @return  The corresponding system function code of action.
   */
  public String getFunctionCode() {
    return(SystemFunctionConstant.STAFF_HIERARCHY);
  }
  
  /**
   *  Method execute() - get full list data and call super method
   *
   *  @param      mapping               ActionMapping from struts
   *  @param      form                  ActionForm from struts
   *  @param      request               HttpServletReuqest
   *  @param      respond               HttpServletRespond
   *  @return     ActionForward         Return the action forward object for struts
   *  @throws     ServletException      Throws ServletException
   */
  public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws ServletException {
    SessionContainer sessionCon = this.getSessionContainer(request);
    Connection conn = this.getConnection(request);
    StaffHierarchyDAObject dao = new StaffHierarchyDAObject(sessionCon, conn);   
    
    request.setAttribute("fullStaffList", dao.getFullList());
    return(super.execute(mapping, form, request, response));
  }

  public void deleteListData(ActionMapping mapping, AbstractSearchForm form, HttpServletRequest request, HttpServletResponse response) throws ApplicationException {
  	SessionContainer sessionCon = this.getSessionContainer(request);
    Connection conn = this.getConnection(request);
    StaffHierarchyDAObject dao = new StaffHierarchyDAObject(sessionCon, conn);
    ListStaffHierarchyForm staffForm = (ListStaffHierarchyForm)form;
    
  	//delete list date
    String[] basicSelectID = staffForm.getBasicSelectedID(); 
    
  	if (!Utility.isEmpty(basicSelectID)){
  		StaffRecordDAObject staffDAO = new StaffRecordDAObject(sessionCon,conn);
  		StaffHierarchyDAObject staffHierarchyDAO = new StaffHierarchyDAObject(sessionCon,conn);
  		String[] tmpBasicSelectID = new String[basicSelectID.length];
  		int tmpBasicSelectIndex = 0;
  		StaffHierarchy staffHierachy = null;
  		
  		for (int k =0;k < basicSelectID.length; k++){
  			if (Utility.isEmpty(basicSelectID[k])) continue;
  			Integer staffHierarchyID = TextUtility.parseIntegerObj(basicSelectID[k]);
  			staffHierachy = (StaffHierarchy)staffHierarchyDAO.getObjectByID(staffHierarchyID);
  			
  			if (Utility.isEmpty(staffHierachy)) continue;
  			List staffList = staffDAO.getStaffRecordListByLevelFieldValueLevelFieldLabel(basicSelectID[k],staffHierachy.getLevelNo().toString());
  			
  			if (Utility.isEmpty(staffList)){
  				tmpBasicSelectID[tmpBasicSelectIndex] = basicSelectID[k];
  				++tmpBasicSelectIndex;
  			} else {
  				this.addError(request, "common.message.records_not_deleted", staffHierachy.getName());
  			}
  		}
  		
  		if (tmpBasicSelectIndex > 0){
  			String[] tmp = new String[tmpBasicSelectIndex];
  			for (int m = 0; m < tmpBasicSelectIndex; m++){
    			tmp[m] = tmpBasicSelectID[m]; 
    			staffForm.setBasicSelectedID(tmp);
    		}
  		} else {
  			form.setBasicSelectedID(null);
  		}
  		
  	}
  	super.deleteListData(mapping,staffForm,request,response);
  	
    //get Company Hierarchy tree
  	request.setAttribute("fullStaffList", dao.getFullList());
  }
}
